<template>
  <div class="update-birthday">
    <!--
          currentDate:双向绑定时间
          min-data:可选的最小日期
          max-data:可选的最大日期
       -->
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择生日"
      :min-date="minDate"
      :max-date="maxDate"
      @confirm="onConfirm"
      @cancel="$emit('close')"
    />
  </div>
</template>

<script>
import dayjs from 'dayjs'
export default {
  data() {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(this.value)
    }
  },
  props: {
    value: {
      type: String,
      required: true
    }
  },
  methods: {
    async onConfirm(value) {
      // 加载中
      this.$toast.loading({
        message: '保存中',
        forbidClick: false,
        duration: 0
      })
      // 请求修改数据
      try {
        const currentDate = dayjs(value).format('YYYY-MM-DD')
        console.log(currentDate)
        await this.$api.updateUSerProfile({
          birthday: currentDate
        })
        // 更新视图
        this.$emit('input', currentDate)
        // 关闭弹层
        this.$emit('close')
        // 提示信息
        this.$toast('更新成功')
      } catch (err) {
        // console.log(err)
        this.$toast('更新失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .van-picker__toolbar {
  button {
    color: #3296fa;
  }
}
</style>
